package com.lyq.database;

import com.lyq.bean.mySql.MysqlLinking;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ViewCourseData {
    public static void main(String[] args) {
        // 创建数据库连接
        MysqlLinking mysqlLinking = new MysqlLinking();
        mysqlLinking.getLink();
        Connection conn = MysqlLinking.conn;
        
        if (conn != null) {
            System.out.println("数据库连接成功！");
            
            try {
                // 查询course表的数据
                String sql = "SELECT C_No, C_Name, C_Time FROM course";
                PreparedStatement ps = conn.prepareStatement(sql);
                ResultSet rs = ps.executeQuery();
                
                System.out.println("当前课程时间数据：");
                System.out.println("课程编号\t课程名称\t\t课程时间");
                System.out.println("----------------------------------------");
                
                while (rs.next()) {
                    String cNo = rs.getString("C_No");
                    String cName = rs.getString("C_Name");
                    String cTime = rs.getString("C_Time");
                    System.out.println(cNo + "\t\t" + cName + "\t\t" + cTime);
                }
                
                rs.close();
                ps.close();
                
                // 关闭数据库连接
                conn.close();
                System.out.println("数据库连接已关闭。");
            } catch (SQLException e) {
                System.err.println("查询课程数据时发生错误: " + e.getMessage());
                e.printStackTrace();
            }
        } else {
            System.err.println("数据库连接失败！");
        }
    }
}